package cn.runagain.demo.wechatminiappdemo.controller;

import cn.runagain.demo.wechatminiappdemo.aes.AesCodec;
import cn.runagain.demo.wechatminiappdemo.repository.UserRepository;
import cn.runagain.demo.wechatminiappdemo.utils.ShellUtil;
import cn.runagain.demo.wechatminiappdemo.utils.WechatDataDecrypt;
import cn.runagain.demo.wechatminiappdemo.utils.WechatDataDecrypt1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/6/13 下午5:23
 */
@RestController
public class TestController {

    private Logger logger = LoggerFactory.getLogger(TestController.class);
    @Autowired
    private UserRepository userRepository;

    /**
     * 测试
     *
     * @return
     */
    @RequestMapping("/test")
    private Object test() {
        logger.info("[test]");
        return ShellUtil.callShell("jps -v");
    }

    @RequestMapping("/test1")
    public Object test01() {
        logger.info("[test1]");
        Map<String, Object> resultMap = new HashMap<>(1);
        resultMap.put("userList", userRepository.findAll());
        return resultMap;
    }

    @RequestMapping("/test2")
    public Object test02() throws Exception {
        String encryptedData = "eE2Gh6efy8mJcJ2zs2sPtWlB2LHMpsCwiYy4M6ydMtraDeQJFWxkxnqgXSiijmzcp9tzOdSCdAyZFPzWnTq8tLDV4fS1S1A9rImmuKC8M6tikZFarCQqvStbHZXr6VFGS/RJmaKkplIT9cKwNOr9frtRJdtLctg3nwkU29ygwe7b+H+HAir+cdJL87J3YNTLLzjhoBDs3qP4Gw+LqstffzXr+bI7ZauqQbBq3LjkOWCraWU3qDtUzZHbaTDJzz6aeT5VM6gwxdeLqhTEcdUbDj9NQDP1bX108V6CbFSrF6h3e5Luhz+YTLw2z9kROb6MBX/O1K7YOLI7QFPwzYWCGwStK63tROgwyIZsv3O/SBedPxc5ihLmSuIXv/SrpayP6+cl7C2KXemux9wniRUgk7FPeBWWKDEGjrvGgJBX/nYTZCnvUs4VGikG6zdsF5fpDqxi3WQIUx3NfrU0VNdCo5IDYYiOrQSaRY27UzSnxrcV2PNeJnOeWzfe0PSCr6K3";
        String sessionKey = "stX+F8y7sqYZBuyai+ofEQ==";
        String iv = "xEFmdJGSxnnnv/rinJzCcg==";
        long start = System.currentTimeMillis();
        System.out.println(WechatDataDecrypt.decrypt(sessionKey, encryptedData, iv));
        System.out.println("耗时:" + (System.currentTimeMillis() - start) + "ms");


        //目标密文
        byte[] encryptDataBytes = org.bouncycastle.util.encoders.Base64.decode(encryptedData);
        //密钥
        byte[] sessionKeyBytes = org.bouncycastle.util.encoders.Base64.decode(sessionKey);
        //解密算法初始向量
        byte[] ivBytes = org.bouncycastle.util.encoders.Base64.decode(iv);

        start = System.currentTimeMillis();
        System.out.println(AesCodec.decrypt(encryptedData, sessionKey, ivBytes));
        System.out.println("解密耗时：" + (System.currentTimeMillis() - start) + "ms");


        //        微信步数加密数据数据
        start = System.currentTimeMillis();
        System.out.println(WechatDataDecrypt1.decrypt(sessionKey, encryptedData, iv));
        System.out.println("耗时:" + (System.currentTimeMillis() - start) + "ms");
        return "1";
    }
}
